﻿ 
using SHKF.B2C.BLL.Center;
using SHKF.B2C.Model.Dto.WareHouse;
using SHKF.B2C.Model.Entity.Center;
using SHKF.B2C.Model.Entity.WareHouse;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
 

namespace SHKF.B2C.BLL.LM
{
    /// <summary>
    /// 库存
    /// </summary>
    public class WareHouseDataBLL : LMBLL
    {
        private readonly ShopBLL shopBLL;

        public WareHouseDataBLL(SqlSugarClient db, 
            ShopBLL shopBLL ) : base(db)
        {
            this.shopBLL = shopBLL;
        }

        public List<V_WareHouseData> Query(int companySN, int shopSN, string keyword)
        {
            List<V_WareHouseData> data = new List<V_WareHouseData>();

            string sql = "select w.*,p.ID, p.Made, p.Spec, p.Unit, p.Model, p.LiceseId, p.Upc, p.ProductName, p.is_sold_out, p.Price from dbo.WareHouseData w ";
                    sql += "inner join PtE_Center.dbo.ProductInfo p on p.ProductSN = w.ProductSN";
            sql += " where w.CompanySN = @CompanySN and w.ShopSN = @ShopSN and p.productname like '%@keyword%'";

            List<SugarParameter> parameters = new List<SugarParameter>();
            parameters.Add(new SugarParameter("CompanySN", companySN));
            parameters.Add(new SugarParameter("ShopSN", shopSN));
            parameters.Add(new SugarParameter("keyword", keyword));

            data = this.db
                .SqlQueryable<V_WareHouseData>(sql)
                .AddParameters(parameters)  
                .ToList();

            return data;
        }

        //反回更新后的总库存
        public Dictionary<long, int> RemoveStockQty(int ShopSN, List<WareHouseDataEntity> data)
        {
            // long ProductSN, int Qty
            Dictionary<long, int> dataList = new Dictionary<long, int>();

            try
            {
                this.db.BeginTran();

                foreach (WareHouseDataEntity updateStock in data)
                {
                    long productSN = updateStock.ProductSN;

                    //更新库存
                    WareHouseDataEntity wareHouse = this.db.Queryable<WareHouseDataEntity>()
                   .Where(oo => oo.ShopSN == ShopSN
                       && oo.ProductSN == productSN)
                   .First();

                    int qty = 0;

                    //如果有库存，更新
                    if (wareHouse != null)
                    {
                        wareHouse.Qty += updateStock.Qty;
                        int result = this.db.Updateable<WareHouseDataEntity>(wareHouse)
                         .ExecuteCommand();

                        qty = wareHouse.Qty;
                    }
                    else //如果没有库存增加一条新的
                    {
                        WareHouseDataEntity newInfo = new WareHouseDataEntity();
                        newInfo.ProductSN = updateStock.ProductSN;
                        newInfo.Qty = updateStock.Qty;
                        newInfo.ShopSN = ShopSN;
                        newInfo.pDate = DateTime.Now;

                        List<ShopEntity> shop = this.shopBLL.Query(ShopSN);
                        if (shop != null)
                        {
                            newInfo.CompanySN = shop[0].CompanySN;
                        }

                        long SN = this.db.Insertable<WareHouseDataEntity>(newInfo)
                        .ExecuteReturnBigIdentity();

                        qty = updateStock.Qty;
                    }

                    if (!dataList.ContainsKey(productSN))
                    {
                        dataList.Add(productSN, qty);
                    }
                }


                this.db.CommitTran();
            }
            catch (Exception ex)
            {
                this.db.RollbackTran();
                throw ex;
            }
            
            return dataList;
        }
    }
}
